import axious from 'axios'
import Qs from 'qs'

/**
 *
 * @param url 路由地址
 * @param data 参数
 * @param change 是否需要qs转换--》跨域解决
 * @param isUpload 是否为图片上传
 * @returns {Promise<unknown>}
 */
const ajax = (url,data={},change=true,isUpload=false) => {
  let type = ''
  JSON.stringify(data)=='{}'?(type='get'):(type='post')
  if(isUpload){
    type='post'
  }
  return new Promise((resolve, reject) => {
    let promise;
    let dataObj = {}
    change?(dataObj=Qs.stringify(data)):(dataObj=data)
      promise = axious({
        method: type,
        url: url,
        data:dataObj
      })
    promise.then(response=>{
      resolve(response.data)
    }).catch(error=>{
      reject(error)
    })
  })
}
export default ajax;
